package com.dces.jx.billtest.xwManageList.service;
import com.dces.jx.billtest.dto.xwManageHead.DcBillHeadXwtParam;
import com.dces.jx.billtest.dto.xwManageList.*;
import com.dces.jx.billtest.xwManageList.dao.DcBillListXwtMapper;
import com.dces.jx.billtest.xwManageList.mapper.DcBillListXwtDtoMapper;
import com.dces.jx.billtest.xwManageList.model.DcBillListXwt;
import org.apache.commons.collections4.CollectionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xdo.common.base.service.BaseService;
import com.xdo.common.token.UserInfoToken;
import com.xdo.domain.PageParam;
import com.xdo.domain.ResultObject;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.UUID;
import java.util.stream.Collectors;
/**
 * generated by Generate dcits
 * Service;
 *
 * @author xdoV2.0
 * @date: 2021-9-13
 */
@Service
public class DcBillListXwtService extends BaseService<DcBillListXwt> {
    @Resource
    private DcBillListXwtMapper dcBillListXwtMapper;
    @Resource
    private DcBillListXwtDtoMapper dcBillListXwtDtoMapper;
    @Override
    public Mapper<DcBillListXwt> getMapper() {
        return dcBillListXwtMapper;
    }
    /**
     * 功能描述: grid分页查询
     *
     * @param dcBillListXwtParam
     * @param pageParam
     * @return
     */
    public ResultObject<List<DcBillListXwtDto>> selectAllPaged(DcBillListXwtParam dcBillListXwtParam, PageParam pageParam) {
        // 启用分页查询
        DcBillListXwt dcBillListXwt = dcBillListXwtDtoMapper.toPo(dcBillListXwtParam);
        Page<DcBillListXwt> page = PageHelper.startPage(pageParam.getPage(), pageParam.getLimit(), pageParam.getSortOrderContent())
                .doSelectPage(() -> dcBillListXwtMapper.getList(dcBillListXwt));
        List<DcBillListXwtDto> dcBillListXwtDtos = page.getResult().stream().map(head -> {
            DcBillListXwtDto dto = dcBillListXwtDtoMapper.toDto(head);
            return dto;
        }).collect(Collectors.toList());
		 ResultObject<List<DcBillListXwtDto>> paged = ResultObject.createInstance(dcBillListXwtDtos, (int) page.getTotal(), page.getPageNum());
        return paged;
    }
    /**
     * 功能描述:查询所有数据
     *
     * @param exportParam
     * @return
     */
    public List<DcBillListXwtDto> selectAll(DcBillListXwtParam exportParam) {
        DcBillListXwt dcBillListXwt = dcBillListXwtDtoMapper.toPo(exportParam);
        List<DcBillListXwtDto> dcBillListXwtDtos = new ArrayList<>();
        List<DcBillListXwt> dcBillListXwts = dcBillListXwtMapper.getList(dcBillListXwt);
        if (CollectionUtils.isNotEmpty(dcBillListXwts)) {
            dcBillListXwtDtos = dcBillListXwts.stream().map(item -> {
                DcBillListXwtDto dto = dcBillListXwtDtoMapper.toDto(item);
                return dto;
            }).collect(Collectors.toList());
        }
        return dcBillListXwtDtos;
    }
    /**
     * 功能描述:新增
     *
     * @param model
     * @param userInfo
     * @return
     */
    public DcBillListXwtDto insert(DcBillListXwtParam model, UserInfoToken userInfo) {
        String sid = UUID.randomUUID().toString();
        DcBillListXwt dcBillListXwt = dcBillListXwtDtoMapper.toPo(model);
        //表体添加数据时自动添加序号
        dcBillListXwt.setSerialNo(dcBillListXwtMapper.getMaxSerialNo(dcBillListXwt));
        //经营单位编码-TRADE_CODE：默认当前企业编码
        dcBillListXwt.setTradeCode(userInfo.getCompany());
        dcBillListXwt.setSid(sid);
        dcBillListXwt.setInsertUser(userInfo.getUserNo());
        dcBillListXwt.setInsertTime(new Date());
        int insertStatus = dcBillListXwtMapper.insert(dcBillListXwt);
       return insertStatus > 0 ? dcBillListXwtDtoMapper.toDto(dcBillListXwt) : null;
    }
    /**
     * 功能描述:修改
     *
     * @param dcBillListXwtParam
     * @param userInfo
     * @return
     */
    public DcBillListXwtDto update(DcBillListXwtParam dcBillListXwtParam, UserInfoToken userInfo) {
        DcBillListXwt dcBillListXwt = dcBillListXwtMapper.selectByPrimaryKey(dcBillListXwtParam.getSid());
        dcBillListXwtDtoMapper.updatePo(dcBillListXwtParam, dcBillListXwt);
        dcBillListXwt.setUpdateUser(userInfo.getUserNo());
        dcBillListXwt.setUpdateTime(new Date());
        // 更新数据
        int update = dcBillListXwtMapper.updateByPrimaryKey(dcBillListXwt);
         return update > 0 ? dcBillListXwtDtoMapper.toDto(dcBillListXwt) : null;
    }
    /**
     * 功能描述:批量删除
     *
     * @param sids
     * @return
     */
    public void delete(List<String> sids, UserInfoToken userInfo) {
        dcBillListXwtMapper.deleteBySids(sids);
    }
	public int getListNumByHeadIds(List<String> sids) {
        return dcBillListXwtMapper.getListNumByHeadIds(sids);
    }

    /**
     * 查询输入料号是否备案返回料件信息
     * @param emsCusImgCurParam
     * @return
     */
    public EmsCusImgCurDto getCopGNoIsNull(EmsCusImgCurParam emsCusImgCurParam) {
        return dcBillListXwtMapper.getCopGNoIsNull(emsCusImgCurParam);
    }

    /**
     * 查询输入料号是否备案返回成品信息
     * @param emsCusImgCurParam
     * @return
     */
    public EmsCusExgCurDto getExgCurr(EmsCusImgCurParam emsCusImgCurParam) {
        return dcBillListXwtMapper.getExgCurr(emsCusImgCurParam);
    }

    /**
     * 输入备案序号查询是否备案和返回成品信息
     * @param emsCusImgCurParam
     * @return
     */
    public EmsCusExgCurDto getExgCurrByGNo(EmsCusImgCurParam emsCusImgCurParam) {
        return dcBillListXwtMapper.getExgCurrByGNo(emsCusImgCurParam);
    }

    /**
     * 输入备案序号查询是否备案和返回料件信息
     * @param emsCusImgCurParam
     * @return
     */
    public EmsCusImgCurDto getImgCurrByGNo(EmsCusImgCurParam emsCusImgCurParam) {
        return dcBillListXwtMapper.getImgCurrByGNo(emsCusImgCurParam);
    }

    /**
     * 查询数据汇总信息,根据headID
     * @param dcBillHeadXwtParam
     * @return
     */
    public DcBillListXwtDto getDataSummaryByHeadId(DcBillHeadXwtParam dcBillHeadXwtParam) {
        //TODO 此方法sql语句中加入判空后再进行SUM操作
        return dcBillListXwtMapper.getDataSummaryByHeadId(dcBillHeadXwtParam);
    }
}
